package com.example.flight_ticket_app.Utils;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.flight_ticket_app.Mapper.FlightMapper;
import com.example.flight_ticket_app.Mapper.PlaneMapper;
import com.example.flight_ticket_app.bean.Flight;
import com.example.flight_ticket_app.bean.Plane;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class ThreadUtil {

    @Autowired
    private  FlightMapper flightMapper;

    @Autowired
    private  PlaneMapper planeMapper;

    public  void createFlightThread(Flight flight){
        new Thread(()->{
            while (true){
                if(new Date().after(flight.getArriveDate())){
                    flight.setArrived(1);
                    flightMapper.updateById(flight);
                    planeMapper.update(null, new UpdateWrapper<Plane>().eq("plane_id",flight.getPlaneId()).set("status",0));
                    break;
                }
            }
        }).start();
    }

}
